public class MoneyMessage extends Message {

    private DialogHB dialoghb;
    private String desc;
    private String data;

    public MoneyMessage(TIMMessage message){
        this.message = message;
    }

    public MoneyMessage(String sampleXml){
        message = new TIMMessage();
        TIMCustomElem elem = new TIMCustomElem();
        elem.setDesc(sampleXml);
        message.addElement(elem);
    }

    @Override
    public void showMessage(ChatAdapter.ViewHolder viewHolder,final Context context) {
        clearView(viewHolder,true,context);
        TIMCustomElem elem = (TIMCustomElem) message.getElement(0);
        String msg = elem.getDesc();
        //这里偷懒暂用字符代替，正式工程中往往套入json，或者继承CustomMessage
        if(msg.substring(0,1).equals("a")){
            //金钱消息
            msg=msg.substring(1);
            if(msg.substring(0,7).equals("moneyhd")){
                checkhd(msg,viewHolder,context);
            }else if(msg.substring(0,7).equals("moneyhb")){
                checkhb(msg,viewHolder,context);
            }else if(msg.substring(0,7).equals("moneysd")){
                checksd(msg,viewHolder,context);
            }else if(msg.substring(0,7).equals("moneyzz")){
                checkzz(msg,viewHolder,context);
            }
        }else if(msg.substring(0,1).equals("b")){
            //gif消息
            msg=msg.substring(1);
            checkgif(msg,viewHolder,context);
        }else if(msg.substring(0,1).equals("c")){
            //位置消息
            msg=msg.substring(1);
            String weizhi=msg.substring(msg.indexOf("@") + 1, msg.lastIndexOf("#"));
            String jindu=msg.substring(msg.indexOf("#") + 1, msg.lastIndexOf("$"));
            String weidu=msg.substring(msg.indexOf("$") + 1, msg.lastIndexOf("%"));
            checkLocation(weizhi,jindu,weidu,viewHolder,context);
        }
        showStatus(viewHolder);
    }
   
    @Override
    public String getSummary() {
        TIMCustomElem elem = (TIMCustomElem) message.getElement(0);
        String xiaoxi = elem.getDesc().toString();
        if(xiaoxi.substring(0,1).equals("a")){
            xiaoxi=xiaoxi.substring(1);
            //金钱
            if(xiaoxi.substring(0,7).equals("moneyhd")){
                if(message.isSelf()){
                    xiaoxi="你已领取红包";
                }else {
                    xiaoxi="朋友领取了你的红包";
                }
            }else if(xiaoxi.substring(0,7).equals("moneyzz")){
                if(message.isSelf()){
                    xiaoxi="[转账]待好友收钱";
                }else {
                    xiaoxi="[转账]请确认收钱";
                }
            } else if(xiaoxi.substring(0,7).equals("moneyhb")){
                xiaoxi="[微信红包]恭喜发财，大吉大利";
            }else if(xiaoxi.substring(0,7).equals("moneysd")){
                if(message.isSelf()){
                    xiaoxi="[转账]已收钱";
                }else {
                    xiaoxi="[转账]朋友已收钱";
                }
            }
        }else if(xiaoxi.substring(0,1).equals("b")){
            //gif
            xiaoxi=xiaoxi.substring(1);
            int a=Integer.valueOf(xiaoxi);
            xiaoxi= EmoticonUtil.emoticonData2[a];
        }else{
            //位置
            xiaoxi="[位置]";
        }
        return xiaoxi;
    }

    @Override
    public void save() {
    }

    private void checkzz(String stringBuilder, ChatAdapter.ViewHolder viewHolder, final Context context){
        //检查转账消息 若存在则显示为收到view
        String test=stringBuilder.toString();
        final String danhao=test.substring(test.indexOf("#")+1,test.lastIndexOf("@"));
        final String qian=test.substring(test.indexOf("@")+1,test.length());

        if(MoneyUtil.check_dh(danhao)){
            if(message.isSelf()){
                getBubbleView(viewHolder,true,context).addView(BubleViewUtil.shoudao_view(0,qian,false));
                getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(context, Wx_deal2.class);
                        intent.putExtra("qian",qian);
                        intent.putExtra("n",MyApplication.getInstance().getNow_name());
                        context.startActivity(intent);
                    }
                });
            }else {
                getBubbleView(viewHolder,true,context).addView(BubleViewUtil.shoudao_view(1,qian,false));
                getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(context, Wx_deal2.class);
                        intent.putExtra("qian",qian);
                        intent.putExtra("n","你");
                        context.startActivity(intent);
                    }
                });
            }
        }else {
            if(message.isSelf()){
                getBubbleView(viewHolder,true,context).addView(BubleViewUtil.sb(0,qian));
                getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(context, Wx_deal4.class);
                        intent.putExtra("qian",qian);
                        intent.putExtra("n",MyApplication.getInstance().getNow_name());
                        context.startActivity(intent);
                    }
                });
            }else {
                getBubbleView(viewHolder,true,context).addView(BubleViewUtil.sb(1,qian));
                getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(context, Wx_deal3.class);
                        intent.putExtra("qian",qian);
                        intent.putExtra("danhao",danhao);
                        ((ChatActivity)context).startActivityForResult(intent,8853);
                    }
                });
            }
        }
    }
    private void checksd(String stringBuilder, ChatAdapter.ViewHolder viewHolder,final Context context){
        String test=stringBuilder.toString();
        final String danhao=test.substring(test.indexOf("#")+1,test.lastIndexOf("@"));
        final String qian2=test.substring(test.indexOf("@")+1,test.length());
        //新的收到消息出现时 同步更新本地对应的转账消息，让他变化为收到消息
        if(!MoneyUtil.check_dh(danhao)){
            MyApplication.AllDanhao+=danhao;
            Intent intentS = new Intent(context,UpService.class);
            intentS.putExtra("n",danhao);
            context.startService(intentS);
        }
        if(message.isSelf()){
            getBubbleView(viewHolder,true,context).addView(BubleViewUtil.shoudao_view(0,qian2,false));
            getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(context, Wx_deal2.class);
                    intent.putExtra("qian",qian2);
                    intent.putExtra("n","你");
                    context.startActivity(intent);
                }
            });
        }else {
            getBubbleView(viewHolder,true,context).addView(BubleViewUtil.shoudao_view(1,qian2,false));
            getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(context, Wx_deal2.class);
                    intent.putExtra("qian",qian2);
                    intent.putExtra("n",MyApplication.getInstance().getNow_name());
                    context.startActivity(intent);
                }
            });
        }
    }
    private void checkhb(String stringBuilder, ChatAdapter.ViewHolder viewHolder,final Context context){
        //检查红包消息 若存在则显示为收到红包view
        String test=stringBuilder.toString();
        final String danhao3=test.substring(test.indexOf("#")+1,test.lastIndexOf("@"));
        final String qian3=test.substring(test.indexOf("@")+1,test.length());
        if(MoneyUtil.check_dh(danhao3)){
            if(message.isSelf()){
                getBubbleView(viewHolder,true,context).addView(BubleViewUtil.shoudaohb_view(0));
                getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(context, Wx_hb_deal3.class);
                        intent.putExtra("qian",qian3);
                        context.startActivity(intent);
                    }
                });
            }else {
                getBubbleView(viewHolder,true,context).addView(BubleViewUtil.shoudaohb_view(1));
                getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(context, Wx_hb_deal2.class);
                        intent.putExtra("qian",qian3);
                        intent.putExtra("danhao",danhao3);
                        intent.putExtra("needadd","no");
                        context.startActivity(intent);
                    }
                });
            }
        }else {
            if(message.isSelf()){
                getBubbleView(viewHolder,true,context).addView(BubleViewUtil.hongbao_view(0));
                getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(context, Wx_hb_deal1.class);
                        intent.putExtra("qian",qian3);
                        context.startActivity(intent);
                    }
                });
            }else {
                getBubbleView(viewHolder,true,context).addView(BubleViewUtil.hongbao_view(1));
                getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialoghb=new DialogHB(context,qian3,danhao3);
                        dialoghb.show();
                    }
                });
            }
        }
    }
    private void checkhd(String stringBuilder, ChatAdapter.ViewHolder viewHolder,final Context context){
        String test=stringBuilder.toString();
        final String danhao=test.substring(test.indexOf("#")+1,test.lastIndexOf("@"));
        if(!MoneyUtil.check_dh(danhao)){
            MyApplication.AllDanhao+=danhao;
            Intent intentS = new Intent(context,UpService.class);
            intentS.putExtra("n",danhao);
            context.startService(intentS);
        }
        getBubbleView(viewHolder,false,context);
    }

    private void checkgif( String stringBuilder,ChatAdapter.ViewHolder viewHolder,final Context context){
        getBubbleView(viewHolder,true,context).addView(BubleViewUtil.gifImageView(stringBuilder));
    }

    private void checkLocation(final String weizhi,final String jindu,final String weidu,ChatAdapter.ViewHolder viewHolder,final Context context){
        getBubbleView(viewHolder,true,context).addView(BubleViewUtil.getLoactionView(weizhi,message.isSelf));
        getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, Wx_Location2.class);
                intent.putExtra("jvti",weizhi);
                intent.putExtra("jingdu",jindu);
                intent.putExtra("weidu",weidu);
                context.startActivity(intent);
            }
        });
    }
}
